<?php

App::uses('AppController', 'Controller');

/**
 * Groups Controller
 *
 * @property Group $Group
 */
class StartdayController  extends AppController {

    function beforeFilter() {
    	$this->layout = "satrtday"; 
        $this->Auth->allow('index','end');	 
        parent::beforeFilter();        
       
    }    
	
    public function index() {
    	if($this->Session->read('logTestLv1')!=1){
            $this->redirect("/clarithromycin/confrim");
        }
    	$fullData = $this->Session->read('dataFull');  
    	
    	if(count($fullData)==0){
    		 $this->redirect("/clarithromycin/confrim");
    	} 	
    	  if ($this->request->is('post')) {    	  	
    	  	$chekOne=explode(",", $_POST['ansewId']);
    	  	//pr($chekOne);
    	  	if(isset($chekOne[1]) && $chekOne[1]==0 && $_POST['stop']==1){
    	  		$datas = $fullData[$_POST['numberNext']];
    	  	}elseif(isset($chekOne[1]) && $chekOne[1]==0 && $_POST['stop']==0){
    	  		$this->redirect('end');
    	  	}else if(isset($chekOne[1]) && $chekOne[1]==1){
    	  		
    	  		 $this->loadModel('QuestionAnswer');
    	  		 
    	  		  $checkInsert = $this->QuestionAnswer->find('all',array(
					'conditions'=>array(
						'QuestionAnswer.question_id'=>$_POST['idQuest'],
						'QuestionAnswer.post_id'=>$this->Session->read('idMemberlv1'),
						'QuestionAnswer.answer_id'=>$chekOne[0],
					)
			 	 ));
    	  		 if (count($checkInsert)==0){
    	  		 $start=strtotime(date($this->Session->read('startTestlv1')));
				 $nowTime = strtotime(date("Y-m-d H:i:s"));         
				 $between = $nowTime - $start;
    	  		 $data = array('QuestionAnswer'=>array(
									  'name_member'=> $this->Session->read('nameMemberlv1'),
									  'post_id'=>$this->Session->read('idMemberlv1'),
    	  		 					  'question_id'=>$_POST['idQuest'],	
									  'answer_id'=>$chekOne[0],
									  'ruler'=>1,
									  'munber'=>$_POST['numberNext'],
									  'total_time'=>$between,
    	  		 					  'modified'=>date("Y-m-d H:i:s"),	
									  'question_end'=>$_POST['stop'],
    	  		 					  'type'=>1	
							  ));
    	  		 }else{
    	  		 	 $start=strtotime(date($this->Session->read('startTestlv1')));
					 $nowTime = strtotime(date("Y-m-d H:i:s"));         
					 $between = $nowTime - $start;
	    	  		 $data = array('QuestionAnswer'=>array(
										  'id'=>$checkInsert[0]['QuestionAnswer']['id'],
										  'total_time'=>$between,
	    	  		 					  'modified'=>date("Y-m-d H:i:s"),	
										  
								  ));
    	  		 }
    	  		 	$this->QuestionAnswer->save($data);			  
					$datas = $fullData[$_POST['numberNext']];
					if($_POST['stop']==0){
						$this->redirect('end');
					}			  
    	  	}
    	  	
    	  	
    	  }else{
    	  	$datas =$fullData[0];
    	  }
    	//fulldata satrtday.ctp
    	 $this->set('data',$datas);
        $this->render('/Fontend/Startday/index');
        
    }
    
    public function end() {
    	 $this->layout = "template"; 
        $this->render('/Fontend/Startday/end');        
    }
}
?>